package com.zxb.core;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.zxb.core.common.R;
import com.zxb.core.common.enums.BaseEnumService;
import com.zxb.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Zxb
 */
@RestController
@ApiIgnore
public class IndexController {
    @Value("${spring.application.name}")
    private String serviceName;

    @GetMapping("/")
    public R<String> index() {
        return R.success("后端服务" + serviceName + "已经成功启动");
    }
    @GetMapping("/un/enum")
    public R<?> findEnum(String code) {
        ArrayNode an = JsonUtil.an();
        if (StrUtil.isBlank(code)) {
            return R.success(an);
        }
        Map<String, BaseEnumService> map = SpringUtil.getBeansOfType(BaseEnumService.class);
        List<BaseEnumService> list = map.values().stream().filter(t -> code.equals(t.getCode())).collect(Collectors.toList());
        if (CollUtil.isEmpty(list)) {
            return R.success(an);
        }
        return R.success(list.get(0).getList());
    }
}
